//
//  AddTypeBillView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/22.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol AddTypeBillViewDelegate {
    @objc optional func selectBillType(index:Int) //选择账单类型
}

class AddTypeBillView: UIView {
    
    weak var delegate : AddTypeBillViewDelegate?
    
    fileprivate let cellId = "typeBillCell"
    fileprivate let typeArray = [mailBill,otherBill,inputBill]
    fileprivate let textArray = [getBillForMail,getBillForOther,getBillForInput]

    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.white
        
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = .init(top: RATIO_H(maxNum: 20), left: RATIO(maxNum: 20), bottom: RATIO_H(maxNum: 20), right: RATIO(maxNum: 20))
        layout.minimumLineSpacing = RATIO_H(maxNum: 20)
        layout.minimumInteritemSpacing = RATIO(maxNum: 20)
        
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(AddTypeBillCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension AddTypeBillView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return typeArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? AddTypeBillCell
        cell?.titleLabel.text = typeArray[indexPath.row]
        cell?.contextLabel.text = textArray[indexPath.row]
        
        if indexPath.row == 0 {
            cell?.contentView.layer.contents = UIImage.init(named: "bg_mail")?.cgImage
            cell?.titleLabel.textColor = UIColor.white
            cell?.contextLabel.textColor = UIColor.white
        }else{
            cell?.contentView.layer.borderWidth = 1
            cell?.contentView.layer.borderColor = UIColor.lineColor().cgColor
            cell?.contentView.layer.cornerRadius = 10
            cell?.contentView.layer.masksToBounds = true
        }
        
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.delegate?.selectBillType!(index: indexPath.row)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        if indexPath.row == 0 {
            return .init(width: ScreenW-RATIO(maxNum: 40), height: RATIO_H(maxNum: 80))
        }else{
            return .init(width: (ScreenW-RATIO(maxNum: 60))/2, height: RATIO_H(maxNum: 80))
        }
        
        
    }
}
